﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using vgo_vs01.Models;
using vgo_vs01.Models.DoiTuong;
using vgo_vs01.Models.KetNoi;

namespace vgo_vs01.Controllers
{
    public class ThanhVienController : Controller
    {
        //
        // GET: /ThanhVien/

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult DangNhap()
        {
            return View();
        }
        [HttpPost]
        public ActionResult DangNhap(string tendangnhap, string MatKhau)
        {
            ThanhVienModels TvMd = new ThanhVienModels();
            if (TvMd.KTMatKhau(MatKhau, tendangnhap))
            {

                Session["Tendangnhap"] = tendangnhap;
                return RedirectToAction("DoiMatKhau");
            }
            else
            {
                ViewData["loi"] = "Tên đăng nhập hoặc mật khẩu sai";
            }
            return View();
            
        }
        [HttpGet]
        public ActionResult DangKy() 
        {
            return View("DangKy");
        }
        [HttpPost]
        public ActionResult DangKy(ThanhVien thanhVien, string Ngay,string Thang, string Nam, string Ngay1, string Thang1, string Nam1)
        {

            thanhVien.NgaySinh = Convert.ToDateTime(Ngay + "/" + Thang + "/" + Nam);
            thanhVien.NgayCapCMT = Convert.ToDateTime(Ngay1 + "/" + Thang1 + "/" + Nam1);
            ThanhVienModels thanhVienData = new ThanhVienModels();
            thanhVienData.DangKyThanhVien(thanhVien);
            return View();
        }

        public ActionResult hienthi()
        {
            ViewData["tendangnhap"] = "Doi mat khau thanh cong";
            return View();
 
        }
        [HttpGet]
        public ActionResult Suathongtin()
        {

            return View();
        }

        [HttpPost]
        public ActionResult SuaThongTin(string hoten,DateTime ngaysinh, string CMND, string email, string sdt, string diachi, string quequan, string noicap, DateTime ngaycap)
        {
            ThanhVienModels thanhvienMd = new ThanhVienModels();
            ThanhVien tv = new ThanhVien();
            tv.HoTen = hoten;
            tv.NgaySinh = ngaysinh;
            tv.CMND = CMND;
            tv.Email = email;
            tv.SDT = sdt;
            tv.DiaChi = diachi;
            tv.QueQuan = quequan;
            tv.NoiCapCMT = noicap;
            tv.NgayCapCMT = ngaycap;
            thanhvienMd.SuaThongTin(tv);
            return View();
        }
        [HttpGet]
        public ActionResult DoiMatKhau()
        {
            ViewData["tendangnhap"] = Session["Tendangnhap"];
            return View();
        }
        #region DoiMatKhau
        [HttpPost]
        public ActionResult DoiMatKhau(string matkhau, string matkhaumoi)

        {
            ThanhVienModels thanhvienMd = new ThanhVienModels();
            string tendangnhap = Session["Tendangnhap"].ToString();
            if (thanhvienMd.KTMatKhau(matkhau,tendangnhap))
            {
                ThanhVien tv = new ThanhVien();
                tv.TenDangNhap = tendangnhap;
                tv.MatKhau = matkhaumoi;
                thanhvienMd.DoiMatKhau(tv);
                return View("hienthi");
            }
            else
            {
                ViewData["Loi"] = "Mật Khẩu cũ sai";
            }
            return View();
        }
        #endregion
        
       //lay tendangnhap
        public string getUserName(int dstv) 
        {
            DaoCuModels dcMl = new DaoCuModels();
           string tenDangNhap =  dcMl.GetUserName(dstv);
            return tenDangNhap;
        }
        //lay tendangnhap
        public string GetEmail(int dstv)
        {
            DaoCuModels dcMl = new DaoCuModels();
            string email = dcMl.GetEmail(dstv);
            return email;
        }
        #region Xóa thành viên
        public ActionResult XoaThanhVien(int MaThanhVien)
        {
            ThanhVienModels TvMd = new ThanhVienModels();
           
            TvMd.XoaThanhvien(MaThanhVien);
            return View();
 
        }
        #endregion
    }
    
}
